import { getConversation } from '../../apis/user'

const getDefaultState = () => {
  return {
    // 融云连接状态
    connect: false,
    // 会话列表
    conversation: {},
    // 融云会话列表
    conversationList: [],
    // 单聊记录
    record: {},
    // 获取单次会话记录 用来控制第一次成功请求会话列表
    conversationNum: 0,
    // 会话未读总数
    totalUnreadCount: 0,
    // hasMore
    hasMore: false,
    // historyTimestamp
    historyTimestamp: 0,
    //新会话未读总数
    unReadCount: 0
  }
}

const state = getDefaultState()

const mutations = {
  SET_UNREADCOUNT: (state, unReadCount) => {
    state.unReadCount = unReadCount
  },
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },

  SET_CONNECT: (state, connect) => {
    state.connect = connect
  },
  SET_CONVERSATION: (state, conversation) => {
    state.conversation = conversation
  },
  SET_CONVERSATION_LIST: (state, conversationList) => {
    state.conversationList = conversationList
  },
  SET_RECORD: (state, record) => {
    state.record = record
  },
  SET_HAS_MORE: (state, hasMore) => {
    state.hasMore = hasMore
  },
  SET_HISTORY_TIMESTAMP: (state, historyTimestamp) => {
    state.historyTimestamp = historyTimestamp
  },
  SET_CONVERSATION_NUM: (state, num) => {
    state.conversationNum = num
  },
  SET_TOTAL_UNREAD_COUNT: (state, count) => {
    state.totalUnreadCount = count
  }

}

const actions = {
  setUnReadCount({ commit }, payload) {
    const { unReadCount } = payload
    commit('SET_UNREADCOUNT', unReadCount)
  },
  setConnect({ commit }, payload) {
    const { connect } = payload
    commit('SET_CONNECT', connect)
  },

  setConversation({ commit }, payload) {
    const { conversation } = payload
    commit('SET_CONVERSATION', conversation)
  },
  setConversationNum({ commit }, payload) {
    const { num } = payload
    commit('SET_CONVERSATION_NUM', num)
  },
  setConversationList({ commit }, payload) {
    const { conversationList } = payload
    commit('SET_CONVERSATION_LIST', conversationList)
  },
  setRecord({ commit }, payload) {
    const { record } = payload
    commit('SET_RECORD', record)
  },
  setHasMore({ commit }, payload) {
    const { hasMore } = payload
    commit('SET_HAS_MORE', hasMore)
  },
  setHistoryTimestamp({ commit }, payload) {
    const { historyTimestamp } = payload
    commit('SET_HISTORY_TIMESTAMP', historyTimestamp)
  },
  setTotalUnreadCount({ commit }, payload) {
    const { totalUnreadCount } = payload
    commit('SET_TOTAL_UNREAD_COUNT', totalUnreadCount)
  },
  getConversation({ commit }, payload) {
    const params = []
    for (const item in payload) {
      params.push(item)
    }

    return new Promise((resolve, reject) => {
      getConversation({ data: params }).then(response => {
        const code = response.code
        const user = response.result
        var list = {}

        if (code === 201) {
          for (const i in user) {
            if (user[i]) {
              const obj = {}
              obj.targetId = i
              obj.username = user[i].username
              obj.avatar = user[i].avatar
              obj.role = user[i].role
              obj.sentTime = payload[i].sentTime
              obj.unreadCount = payload[i].unreadCount
              obj.time = payload[i].time
              list[i] = obj
            }
          }
          // let list2 = [];
          // let keysSorted = Object.keys(list).sort(function (a, b) {
          //   return list[b].sentTime - list[a].sentTime;
          // });
          // for (let i = 0; i < keysSorted.length; i++) {
          //   list2[keysSorted[i]] = list[keysSorted[i]];
          // }
          commit('SET_CONVERSATION', list)
          resolve(true)
        }
        resolve(false)
      }).catch(error => {
        reject(error)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

